#include "i2c.h"
#include "light.h"

#define SADDR 0x44

//读寄存器
uint8_t light_read(uint8_t reg)
{
  uint8_t data;
  i2c_write(SADDR, &reg, 1);
  i2c_read(SADDR, &data, 1);
  return data;
}

//写寄存器
//reg: 寄存器地址
//data: 写入数据
void light_write(uint8_t reg, uint8_t data)
{
  uint8_t buf[2];
  buf[0] = reg;
  buf[1] = data;
  i2c_write(SADDR, buf, 2);
}

void light_init(void)
{
  //将command寄存器的bit7置位（enable ADC-core）
  light_write(0, 1 << 7);
  //配置测量范围（0 to 64000 lux）
  light_write(1, 3 << 2);
}

uint16_t light_value(void)
{
  uint8_t lsb = light_read(4);
  uint8_t msb = light_read(5);
  return (msb << 8 | lsb);
}
